<?php

//include_once("./classes/input_fields.php");
$func_name = "object_edit.php";

if ('POST' == $_SERVER['REQUEST_METHOD']) {
    //Formulardaten erhalten
    log_message('[' . $func_name . ']' . POST_RECIVED);
    log_message('[' . $func_name . ']' . print_r($_POST, true));


    $obj_param = array();

    unset($img_id, $img_chop_x, $img_chop_y, $img_chop_w, $img_chop_h);
    foreach ($_POST as $param_name => $param_value) {
        /* var_dump($param_name);
          var_dump($param_value);
         */
        if ($param_value != '') {
            switch ($param_name) {
                case "obj_status":
                    if ($param_value == "true") {
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 1");
                        $obj_param['obj_status'] = "1";
                    } else {
                        $obj_param['obj_status'] = "0";
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 0");
                    }
                    break;

                case "obj_name":

                    $param_value = substr($param_value, 0, 128);
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;

                    break;

                case "obj_cat_id":

                    $param_value = substr($param_value, 0, 128);
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;

                    break;

                case "obj_man":
                    $param_value = substr($param_value, 0, 128);
                    $obj_param['obj_man_id'] = add_field_input("manufacture", $param_value);
                    log_message('[' . $func_name . ']' . "Setze obj_man_id auf: " . $obj_param['obj_man_id']);
                    break;

                case "obj_lable":
                    $param_value = substr($param_value, 0, 128);
                    $obj_lable_id = add_field_input("lable", $param_value);
                    log_message('[' . $func_name . ']' . "Setze obj_lable_id auf: " . $obj_lable_id);
                    $obj_param['obj_lable_id'] = $obj_lable_id;
                    unset($obj_lable_id);
                    break;

                case "obj_artist_draft":
                    $param_value = substr($param_value, 0, 128);
                    $obj_sql_field = 'obj_artist_draft_id';
                    $obj_param[$obj_sql_field] = add_field_input("artist", $param_value);
                    log_message('[' . $func_name . ']' . "Setze $obj_sql_field auf: $obj_param[$obj_sql_field]");
                    break;

                case "obj_artist_design":
                    $param_value = substr($param_value, 0, 128);
                    $obj_sql_field = 'obj_artist_design_id';
                    $obj_param[$obj_sql_field] = add_field_input("artist", $param_value);
                    log_message('[' . $func_name . ']' . "Setze $obj_sql_field auf: $obj_param[$obj_sql_field]");
                    break;

                case "obj_dating":
                    $param_value = substr($param_value, 0, 128);
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "obj_heigh":
                    $param_value = substr($param_value, 0, 128);
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "obj_damage":
                    if ($param_value == "true") {
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 1");
                        $obj_param['obj_damage'] = "1";
                    } else {
                        $obj_param['obj_damage'] = "0";
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 0");
                    }
                    break;

                case "obj_pur_month":
                    $param_value = (int) $param_value;
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "obj_pur_year":
                    $param_value = (int) $param_value;
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "obj_pur_place":
                    $param_value = substr($param_value, 0, 128);
                    $obj_sql_field = 'obj_pur_place_id';
                    $obj_param[$obj_sql_field] = add_field_input("place", $param_value);
                    log_message('[' . $func_name . ']' . "Setze $obj_sql_field auf: $obj_param[$obj_sql_field]");
                    break;

                case "obj_pur_price":
                    $param_value = str_replace(",", ".", $param_value);
                    $param_value = (float) $param_value;

                    switch ($_POST['obj_pur_currency']) {
                        case "DM":
                            $param_value = $param_value * DM_TO_EUR;
                            break;

                        default:
                            break;
                    }

                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    log_message('[' . $func_name . ']' . "Preis: $param_value");
                    break;


                case "obj_pur_currency":
                    if ($param_value != "EUR") {
                        log_message('[' . $func_name . ']' . "Umrechnung erforderlich!");
                    } else {
                        log_message('[' . $func_name . ']' . "Umrechnung nicht erforderlich!");
                    }

                    break;


                case "obj_ancestor_person":
                    $param_value = substr($param_value, 0, 128);
                    $obj_sql_field = 'obj_ancestor_person_id';
                    $obj_param[$obj_sql_field] = add_field_input("person", $param_value);
                    log_message('[' . $func_name . ']' . "Setze $obj_sql_field auf: $obj_param[$obj_sql_field]");
                    break;

                case "obj_present":
                    if ($param_value == true) {
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 1");
                        $obj_param[$param_name] = "1";
                    } else {
                        $obj_param[$param_name] = "0";
                        log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: 0");
                    }
                    break;

                case "obj_present_person":
                    $param_value = substr($param_value, 0, 128);
                    $obj_sql_field = 'obj_present_person_id';
                    $obj_param[$obj_sql_field] = add_field_input("person", $param_value);
                    log_message('[' . $func_name . ']' . "Setze $obj_sql_field auf: $obj_param[$obj_sql_field]");
                    break;

                case "obj_comment":

                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "obj_img_id":
                    $param_value = (int) ($param_value);
                    $img_id = $param_value;
                    log_message('[' . $func_name . ']' . "Setze " . $param_name . " auf: " . $param_value);
                    $obj_param[$param_name] = $param_value;
                    break;

                case "img_chop_x":
                    $img_chop_x = (int) ($param_value / IMAGE_VIEW_RATIO);
                    break;

                case "img_chop_y":
                    $img_chop_y = (int) ($param_value / IMAGE_VIEW_RATIO);
                    break;

                case "img_chop_w":
                    $img_chop_w = (int) ($param_value / IMAGE_VIEW_RATIO);
                    break;

                case "img_chop_h":
                    $img_chop_h = (int) ($param_value / IMAGE_VIEW_RATIO);
                    break;

                default:
                    log_message('[' . $func_name . ']' . "Keine Verwendung fuer: " . $param_name);
                    break;
            }
        }
    }
    if (
            (isset($img_id))
            && (isset($img_chop_x))
            && (isset($img_chop_y))
            && (isset($img_chop_w))
            && (isset($img_chop_h))
    ) {
        $img_id_new = chop_image($img_id, $img_chop_x, $img_chop_y, $img_chop_w, $img_chop_h);
        log_message('[' . $func_name . ']' . "Choped! Setze 'obj_img_id' auf: " . $img_id_new);
        $obj_param['obj_img_id'] = $img_id_new;
        unset($img_id, $img_chop_x, $img_chop_y, $img_chop_w, $img_chop_h);
    }

    $obj_id = add_object($obj_param);
    log_message('[' . $func_name . ']' . "Objekt Angelegt! ID: " . $obj_id);
    /*
      $to_tpl = array();
      $to_tpl["filename"] = 'msg_success.tpl';
      $to_tpl["data"] = array();
     */

    $to_tpl = array();
    $to_tpl["filename"] = 'msg_success.tpl';
    $to_tpl["data"] = array();
    $to_tpl["data"]["msg"] = "Objekt erfolgreich angelegt!";
    $to_tpl['data']['fwd_url'] = '?section=china&action=view&obj_id=' . $obj_id;


    unset($ret_obj);
} else {
    /**
     * @todo Eingehende Daten filtern: $_GET[obj_id]	
     * 
     */
    $to_tpl = array();
    $to_tpl['filename'] = 'object_edit.tpl';
    $to_tpl['data'] = array();
    $to_tpl['data']['object'] = get_object($_GET["obj_id"]);
    $to_tpl['data']['object']["obj_img_url"] = get_image($to_tpl['data']['object']["obj_img_id"]);
}
unset($func_name);
return $to_tpl;
?>

